package com.huangmb.anymark.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import com.huangmb.anymark.app.R;
import com.huangmb.anymark.app.activity.base.AppBaseActivity;
import com.huangmb.anymark.app.fragment.add.AddPlaceFragment;
import com.huangmb.anymark.app.fragment.marker.FoodShopFragment;
import com.huangmb.anymark.app.fragment.user.UserCenterFragment;
import com.huangmb.anymark.app.model.UserMarker;

/**
 * @author hmb
 * @version 1.0
 * @since 2016/4/18 22:18
 */
public class UserCenterActivity extends AppBaseActivity {
    private Toolbar mToolbar;
    private UserCenterFragment mUserCenterFragment;

    @Override
    protected void onInitView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_usercenter);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

    }

    @Override
    protected void afterInitView(Bundle savedInstanceState) {
        super.afterInitView(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        if(!hasLogin()){
            Intent intent = new Intent(this,LoginActivity.class);
           startActivityForResult(intent,REQUEST_LOGIN);
        }else {
            gotoUserCenterPage();
        }
    }

    /*public void gotoLoginPage() {
        if (mLoginFragment == null) {
            mLoginFragment = new LoginFragment();
        }
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, mLoginFragment)
                .commit();
    }

    public void gotoRegisterPage() {
        if (mRegisterFragment == null) {
            mRegisterFragment = new RegisterFragment();
        }
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, mRegisterFragment)
                .commit();
    }*/

    public void gotoUserCenterPage() {
        if (mUserCenterFragment == null) {
            mUserCenterFragment = new UserCenterFragment();
        }
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, mUserCenterFragment)
                .commit();
    }
    public void gotoDetailPage(UserMarker marker){
        FoodShopFragment fragment = FoodShopFragment.newInstance(marker);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container,fragment)
                .addToBackStack(null)
                .commit();
    }

    public void gotoModifyPage(UserMarker marker){
        AddPlaceFragment fragment = new AddPlaceFragment();
        Bundle b = new Bundle();
        b.putParcelable("marker",marker);
        fragment.setArguments(b);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container,fragment)
                .addToBackStack(null)
                .commit();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_LOGIN) {
            if (resultCode == RESULT_OK) {
                gotoUserCenterPage();
            } else {
                toast("取消登录");
                finish();
            }
        }
    }
}
